#ifndef PROTOCOLCONFIGMANAGER_H
#define PROTOCOLCONFIGMANAGER_H

#include <QObject>
#include <QVariantMap>
#include <QString>
#include <QMap>

class ProtocolConfigManager : public QObject {
    Q_OBJECT
    Q_PROPERTY(QString className READ className CONSTANT)
    Q_INVOKABLE QString className() const { return "ProtocolConfigManager"; }

public:
    explicit ProtocolConfigManager(QObject *parent = nullptr);
    ~ProtocolConfigManager() override;

    // 加载指定设备的配置文件
    Q_INVOKABLE bool loadConfig(const QString &configFilePath, const QString &deviceId = QString());

    // 释放指定设备的配置
    Q_INVOKABLE void releaseConfig(const QString &deviceId);

    // 获取指定设备的二进制协议配置
    Q_INVOKABLE QVariantMap getBinaryProtocolConfig(int commandType, const QString &deviceId = QString()) const;

    // 获取指定设备的函数协议配置
    Q_INVOKABLE QVariantMap getFunctionProtocolConfig(const QString &functionName, const QString &deviceId = QString()) const;

    // 检查配置是否有效
    Q_INVOKABLE bool isConfigValid() const;

    // 根据协议名称获取协议信息
    Q_INVOKABLE QVariantMap getProtocolInfo(const QString &protocolName, const QString &deviceId = QString()) const;

private:
    // 存储所有设备的二进制协议配置 (deviceId -> commandType -> config)
    QMap<QString, QMap<int, QVariantMap>> m_binaryProtocolConfigs;

    // 存储所有设备的函数协议配置 (deviceId -> functionName -> config)
    QMap<QString, QMap<QString, QVariantMap>> m_functionProtocolConfigs;

    // 配置有效性标志 (deviceId -> isValid)
    QMap<QString, bool> m_isConfigValid;

    // 默认设备ID
    QString m_defaultDeviceId;

    // 解析JSON配置
    bool parseJsonConfig(const QByteArray &data, const QString &deviceId);


};

#endif // PROTOCOLCONFIGMANAGER_H